using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The DistrictCodeTableDB class is responsible for interacting with the database to retrieve and store information
/// about DistrictCodeTable objects.
/// </summary>
public class DistrictCodeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of DistrictCodeTable from the underlying datasource.
/// </summary>
/// <param name="districtCode">The unique DistrictCode of the DistrictCodeTable in the database.</param>
/// <returns>An DistrictCodeTable when the DistrictCode was found in the database, or null otherwise.</returns>
public static DistrictCodeTable GetItem(short districtCode)
{
DistrictCodeTable myDistrictCodeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocDistrictCodeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@districtCode", districtCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myDistrictCodeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myDistrictCodeTable;
}
}

/// <summary>
/// Returns a list with DistrictCodeTable objects.
/// </summary>
/// <returns>A generics List with the DistrictCodeTable objects.</returns>
public static DistrictCodeTableList GetList()
{
DistrictCodeTableList tempList = new DistrictCodeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocDistrictCodeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure; 
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

public static DistrictCodeTableList GetList(Int16 countryCode)
{
    DistrictCodeTableList tempList = new DistrictCodeTableList();
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocDistrictCodeTableSelectList", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@CountryCode", countryCode);
            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.HasRows)
                {
                    while (myReader.Read())
                    {
                        tempList.Add(FillDataRecord(myReader));
                    }
                }
                myReader.Close();
            }
        }
    }
    return tempList;
}


/// <summary>
/// Saves a DistrictCodeTable in the database.
/// </summary>
/// <param name="myDistrictCodeTable">The DistrictCodeTable instance to save.</param>
/// <returns>The new DistrictCode if the DistrictCodeTable is new in the database or the existing DistrictCode when an item was updated.</returns>
public static Int16 Save(DistrictCodeTable myDistrictCodeTable)
{
Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocDistrictCodeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myDistrictCodeTable.DistrictCode == -1)
{myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@districtCode", myDistrictCodeTable.DistrictCode);
}
if (myDistrictCodeTable.CountryCode == null){
myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@countryCode", myDistrictCodeTable.CountryCode);
}
if (string.IsNullOrEmpty(myDistrictCodeTable.DistrictName))
{
myCommand.Parameters.AddWithValue("@districtName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@districtName", myDistrictCodeTable.DistrictName);
}
if (string.IsNullOrEmpty(myDistrictCodeTable.DistrictShortName))
{
myCommand.Parameters.AddWithValue("@districtShortName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@districtShortName", myDistrictCodeTable.DistrictShortName);
}
if (string.IsNullOrEmpty(myDistrictCodeTable.DistrictOtherName))
{
myCommand.Parameters.AddWithValue("@districtOtherName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@districtOtherName", myDistrictCodeTable.DistrictOtherName);
}
if (string.IsNullOrEmpty(myDistrictCodeTable.Remark))
{
myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remark", myDistrictCodeTable.Remark);
}
if (myDistrictCodeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myDistrictCodeTable.Status);
}
if (myDistrictCodeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myDistrictCodeTable.CreatedDateTime);
}
if (myDistrictCodeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myDistrictCodeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a DistrictCodeTable from the database.
/// </summary>
/// <param name="districtCode">The DistrictCode of the DistrictCodeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(Int32 districtCode)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocDistrictCodeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@districtCode", districtCode);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the DistrictCodeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static DistrictCodeTable FillDataRecord(IDataRecord myDataRecord )
{
DistrictCodeTable myDistrictCodeTable = new DistrictCodeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
{
myDistrictCodeTable.DistrictCode = myDataRecord.GetInt32(myDataRecord.GetOrdinal("DistrictCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
{
myDistrictCodeTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictName")))
{
myDistrictCodeTable.DistrictName = myDataRecord.GetString(myDataRecord.GetOrdinal("DistrictName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictShortName")))
{
myDistrictCodeTable.DistrictShortName = myDataRecord.GetString(myDataRecord.GetOrdinal("DistrictShortName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictOtherName")))
{
myDistrictCodeTable.DistrictOtherName = myDataRecord.GetString(myDataRecord.GetOrdinal("DistrictOtherName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
{
myDistrictCodeTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myDistrictCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myDistrictCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myDistrictCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myDistrictCodeTable;
}
}

 } 
